<html>
<head>
<title>Custom Source Code</title>
<link rel="stylesheet" type="text/css" href="../media/style.css">
</head>
<body>

<table border="0" cellspacing="0" cellpadding="0" height="48" width="100%">
  <tr>
    <td width="100%">
			<table width="100%" height="78" border="0" align="left" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" id="topnav">
			<tr>
				<!-- Header Side -->
				<td width="17">
					<img height="78" src="media/reportico100.png"/>
				</td>
				<td align="right"></td>
				<td align="center">
					<table width="100%" height="60" border="0" align="left" cellpadding="0" cellspacing="0">
						<tr>
							<td valign="top" align="left">
							<img src="media/repbanner.png"/></td>
							</td>
						</tr>
						<tr>
							<td align="center" valign="bottom" class="topmenucell">
								<nobr>
								&nbsp;
								</nobr>
							</td>
						</tr>
					</table>
				</td>
				<td width="103" class="toplogincell">
					&nbsp;
				</td>
			</tr>
			</table>
</td>
  </tr>
  <!--tr>
    <td class="header_top">reportico</td>
  </tr-->
  <tr><td class="header_line"><img src="../media/empty.png" width="1" height="1" border="0" alt=""  /></td></tr>
  <tr>
    <td class="header_menu">
  		  [ <a href="../classtrees_reportico.html" class="menu">class tree: reportico</a> ]
		  [ <a href="../elementindex_reportico.html" class="menu">index: reportico</a> ]
		  [ <a href="../elementindex.html" class="menu">all elements</a> ]
    </td>
  </tr>
  <tr><td class="header_line"><img src="../media/empty.png" width="1" height="1" border="0" alt=""  /></td></tr>
</table>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="200" class="menu">
      <b>Packages:</b><br />
              <a href="../li_reportico.html">reportico</a><br />
            <br /><br />
		<b>Tutorials/Manuals:</b><br />
					<strong>Package-level:</strong>
							<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.pkg.html">Reportico Tutorial And User Manual</a>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.manual.pkg.html">Reportico User Manual</a>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.quickstart.pkg.html">Reportico Installation</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.gettingstarted.pkg.html">Getting Started</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.structure.pkg.html">Reportico Installation Structure</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.using.pkg.html">Configuring Reportico - System Parameters</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.embedding.pkg.html">Embedding Reportico</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.security.pkg.html">Reportico Security</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.language.pkg.html">Language Support</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.projects.pkg.html">Projects</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.importlink.pkg.html">Importing and linking from reports</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.menus.pkg.html">Report Menus</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.prepare.pkg.html">Report Preparation and Execution</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.design.pkg.html">Report Design Mode</a>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.format.pkg.html">The Design Format Menu</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.customcode.pkg.html">Custom Source Code</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.qrydet.pkg.html">The Query Details Menu</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.assign.pkg.html">The Assignments Menu</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.criteria.pkg.html">The Criteria Menu</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.output.pkg.html">The Output Menu</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.webservices.pkg.html">Turning Reports into Web Services (currently unavailable)</a>
</ul>
</li></ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.faq.pkg.html">Reportico FAQ</a>
</ul>
</li></ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.howto.pkg.html">Reportico Tutorial</a>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.tutorial1.pkg.html">Tutorial No.1 - The Film Listing Report</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.tutorial1_1.pkg.html">Tutorial 1 Stage 1 - Creating a basic report</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.tutorial1_2.pkg.html">Tutorial 1 Stage 2 - Creating User Entry Criteria</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.tutorial1_3.pkg.html">Tutorial 1 Stage 3 - Working with expressions using assignments</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.tutorial1_4.pkg.html">Tutorial 1 Stage 4 - Working with groups</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.tutorial1_5.pkg.html">Tutorial 1 Stage 5 - Including database graphics into the report and generating drilldowns</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.tutorial2_1.pkg.html">Tutorial 2 - Creating the loan history report</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.tutorial3_1.pkg.html">Tutorial 3 - The Monthly Returns Report</a>
</ul>
<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.tutorial4_1.pkg.html">Tutorial 4 - The Late Returns Summary report</a>
</ul>
</li></ul>
</li></ul>

							<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.appendix1.pkg.html">Appendix 1 - Reportico Project Configuration parameters</a>
</ul>

							<ul>
	<li type="square"><a href="../reportico/tutorial_reportico.appendix2.pkg.html">Appendix 2 - Reportico URL request parameters</a>
</ul>

									                        <b>Files:</b><br />
      	  <div class="package">
			<a href="../reportico/_reportico.php.html">		reportico.php
		</a><br>
	  </div><br />
      
                </td>
    <td>
      <table cellpadding="10" cellspacing="0" width="100%" border="0"><tr><td valign="top">

<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="10%" align="left" valign="bottom"><a href=
"../reportico/tutorial_reportico.format.pkg.html">Prev</a></td>
<td width="80%" align="center" valign="bottom"></td>
<td width="10%" align="right" valign="bottom"><a href=
"../reportico/tutorial_reportico.qrydet.pkg.html">Next</a></td>
</tr>
</table>
<span><a name="started.CustomSourceCode"></a><h2 class="title">Custom Source Code</h2><p>You may enter your own PHP code into the Custom Source Code Box on the <strong>Format</strong> menu.</p>

      <p>The code you enter here is executed before the main report query and is 
      used to perform preparatory work that for use by the
      main query. For example, this code may generate and populate temporary tables based
      on the criteria the user has entered, and this may be then linked in by the main report query.
      report query.</p>

      <p>Custom Code is entered for more complex data preparation than <a href="../reportico/tutorial_reportico.qrydet.pkg.html#PreSQLs">PreSQLs</a>
      can provide. If used in conjunction with Pre-SQLs then this
      section is executed after Pre-SQLs  and before the main report query.</p>

      <p><strong class = "bold">Note that custom source code may not be
      entered when running in SAFE mode as the user could create SQL
      statements here that could delete and update data in the
      database. For instructions on how to turn on and off Safe Mode see the <a href="../reportico/tutorial_reportico.security.pkg.html">Reportico Security</a> section.</strong></p>

    <p><div class="title">Functions</div>Functions may be entered into the custom source code.</p>
	<p><div class="title">Includes</div>Your custom code may be included using an <strong>require</strong> or <strong>include</strong> statement instead of being entered into this box. This is convenient as the code can be edited in a more convenient text editor. Also this aids debugging by allowing you to rerun your report while correcting errors in the custom source code without having to enter Design mode each time.</p>
Source is included relative to the top level Reportico directory. It is recommended you create your source file within your report project directory and name it with the same name as the report xml file but with the .php extension. To include your file just enter into the Custom Source box :-
        <p><code><pre>
        include(&quot;projects/&lt;myproject&gt;/&lt;myreport&gt;.php&quot;);
</pre></code></p><hr />
    <p><div class="title">echoes, prints etc</div>As this code is executed before the main report query, any output statements will appear at the top of the report out. Using echo or print statements here can be useful for debugging purposes.</p><hr />
    <p><div class="title">Triggering Error Messages</div>Any errors or warnings you may wish to raise during the course of this code running can be raised to the user using the <strong>trigger_error</strong> function which will inform the user of your own error information after the report is run. The format is as follows :-</p>
        <p><code><pre>
        trigger_error(&quot;&lt;Error Message Test&gt;&quot;, E_USER_ERROR);

	for example :-
        trigger_error(&quot;Error &quot;.$_pdo-&gt;errorCode().&quot; occurred in SQL statement.&quot;, E_USER_ERROR);
</pre></code></p>

      <p><div class="title">Useful Variables</div>Since the code entered here is generally intended to perform database queries
      , the following variables are available</p>

      <ol><li><p><strong class = "bold">$_pdo</strong></p>

          <p>A connection to your database. This is used to
          execute SQLs, to create and populate temporary tables and to perform
          other preparatory work like filtering, reformatting etc. Can also be
          used to perform database maintenance operations such as deletes and updates.
          This is a PDO type connection and so can be used as documented in the PHP PDO documentation at http://php.net/manual/en/book.pdo.php.</p>

          <p>For example, this value creates a temporary table containing month numbers
          and names. This could then be joined in with the main report query
          to generate reports with month names based on a month number from
          another table.</p>

          <p><code><pre>
    $sql =&quot;CREATE TEMPORARY TABLE t_month ( monthno INT, monthname CHAR(12) ) &quot;;
    $stat = $_pdo-&gt;query($sql); 
    if ( !$stat ) 
    {
        trigger_error(&quot;Query Failed&lt;BR&gt;&quot;.$sql.&quot;&lt;br&gt;&quot; . 
              &quot;Error &quot;.$_pdo-&gt;errorCode(). &quot; - &quot;. 
              $_pdo-&gt;errorInfo(), E_USER_ERROR); 
              return; 
    } 

    $sql = &quot;INSERT INTO t_month VALUES (1, 'January')&quot;; 
    $stat = $_pdo-&gt;query($sql); 

    $sql = &quot;INSERT INTO t_month VALUES (1, 'February')&quot;; 
    $stat = $_pdo-&gt;query($sql);

    $sql = &quot;INSERT INTO t_month VALUES (1, 'March')&quot;; 
    $stat = $_pdo-&gt;query($sql); 

    /*.............. And so on for each month ..........*/

    // Also cursors may be used for example :-
    foreach($_pdo-&gt;query('SELECT * from t_month') as $row) 
    {
       print_r($row);
    }

</pre></code></p></li>

        <li><p><strong class = "bold">$_criteria</strong></p>


          <p>This array variable contains an entry for each criteria item
          on the prepare screen and gives access to the relevant selected
          criteria values. This allows query statements to be executed in the
          custom code that incorporate the user's selected criteria. This is
          advantageous in several ways. For example, reports that use queries
          selecting from many large tables will be faster if some of the data
          meeting the entered criteria is first selected into temporary tables
          which can then be linked into the main query at the end. Also data can be
          selected into temporary tables and further filtered or modified
          before the final report query runs.</p>

          <p>Accessing criteria values as selected on the prepare screen
          involves calling the <strong>get_criteria_clause()</strong> function for the relevant
          criteria item passing the type of value you wish returned from the
          criteria which depends on whether the criteria item is a list,
          lookup or date criteria type. The valid passed argument can be one
          of &quot;VALUE&quot;, &quot;RANGE1&quot; or &quot;RANGE2&quot;.</p>

          <p>For LOOKUP criteria types, the parameter of &quot;VALUE&quot; should be
          passed to get the user's selection. For example, with a LOOKUP
          criteria that is named <strong class = "bold">country</strong> that
          presents the user with a set of countries selected from the
          database, you may fetch the selected countries with something like
          this:-</p>

          <p><pre class="listing">$countries =
    $_criteria[&quot;country&quot;]-&gt;get_criteria_value(&quot;VALUE&quot;);</pre></p>

          <p>This returns a comma delimited string containing the selected
          country values that is suitable for use in an SQL IN clause to help
          you use it in further queries within the custom code. The values
          returned relate to the criteria's Return Column attribute.</p>

          <p>For <strong>List</strong> criteria items, again the parameter of &quot;VALUE&quot; should
          be passed to get the user's selection as a comma-separated
          list.</p>

          <p>For Text Field criteria, pass the &quot;VALUE&quot; parameter to get what
          the user has typed in to the criteria text field.</p>

          <p>For Date type criteria, pass the &quot;VALUE&quot; parameter to fetch the chosen
          date. This will be returned in the format as defined in the
          configuration parameter SW_DB_DATEFORMAT. This should be suitable
          for including in SQL data comparison clauses.</p>

          <p>For Date Range Criteria, pass the &quot;RANGE1&quot; parameter to get the lower
          date range value and pass &quot;RANGE2&quot; to get the upper date range
          parameter.</p>

          <p>The get_criteria_value() function by default returns criteria values which are surrounded by single quotes. This is so that they can be incorporated easily into SQL text e.g. 'January','February','March'. You can return the values without the single quotes by passing <strong>false</strong> as a second parameter.</p></li></ol><hr /></span>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="33%" align="left" valign="top"><a href="../reportico/tutorial_reportico.format.pkg.html">Prev</a></td>
<td width="34%" align="center" valign="top"><a href=
"../reportico/tutorial_reportico.design.pkg.html">Up</a></td>
<td width="33%" align="right" valign="top"><a href=
"../reportico/tutorial_reportico.qrydet.pkg.html">Next</a></td>
</tr>

<tr>
<td width="33%" align="left" valign="top">The Design Format Menu</td>
<td width="34%" align="center" valign="top">Report Design Mode</td>
<td width="33%" align="right" valign="top">The Query Details Menu</td>
</tr>
</table>
        <div class="credit">
		    <hr />
		    Documentation generated on Mon, 09 Jun 2014 18:10:07 +0100 by <a href="http://www.phpdoc.org">phpDocumentor 1.4.3</a>
	      </div>
      </td></tr></table>
    </td>
  </tr>
</table>

</body>
</html>